package cn.wjh.business.service.impl;

import cn.wjh.business.domain.Focus;
import cn.wjh.business.mapper.FocusMapper;
import cn.wjh.business.service.IFocusService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * 关注Service业务层处理
 *
 * @author jiraWu
 * @date 2023-10-22
 */
@Service
public class FocusServiceImpl extends ServiceImpl<FocusMapper, Focus> implements IFocusService {
	@Resource
	private FocusMapper focusMapper;

	/**
	 * 查询关注
	 *
	 * @param focusId 关注主键
	 * @return 关注
	 */
	@Override
	public Focus selectFocusByFocusId(Long focusId) {
		return focusMapper.selectFocusByFocusId(focusId);
	}

	/**
	 * 查询关注列表
	 *
	 * @param focus 关注
	 * @return 关注
	 */
	@Override
	public List<Focus> selectFocusList(Focus focus) {
		return focusMapper.selectFocusList(focus);
	}

	/**
	 * 新增关注
	 *
	 * @param focus 关注
	 * @return 结果
	 */
	@Override
	public int insertFocus(Focus focus) {
		Long userId = SecurityUtils.getUserId();
		if (Objects.equals(userId, focus.getReceiveId())) throw new ServiceException("您无法自己关注自己!", 601);

		focus.setCreateTime(DateUtils.getNowDate());
		focus.setCreateBy(SecurityUtils.getUsername());
		focus.setUserId(userId);
		try {
			/*关注*/
			return focusMapper.insertFocus(focus);
		} catch (DuplicateKeyException e) {
			/*取消关注*/
			LambdaQueryWrapper<Focus> deleteWrapper = new QueryWrapper<Focus>().lambda()
					.eq(Focus::getUserId, userId)
					.eq(Focus::getReceiveId, focus.getReceiveId());
			return focusMapper.delete(deleteWrapper);
		}
	}

	/**
	 * 修改关注
	 *
	 * @param focus 关注
	 * @return 结果
	 */
	@Override
	public int updateFocus(Focus focus) {
		return focusMapper.updateFocus(focus);
	}

	/**
	 * 批量删除关注
	 *
	 * @param focusIds 需要删除的关注主键
	 * @return 结果
	 */
	@Override
	public int deleteFocusByFocusIds(Long[] focusIds) {
		return focusMapper.deleteFocusByFocusIds(focusIds);
	}

	/**
	 * 删除关注信息
	 *
	 * @param focusId 关注主键
	 * @return 结果
	 */
	@Override
	public int deleteFocusByFocusId(Long focusId) {
		return focusMapper.deleteFocusByFocusId(focusId);
	}

	@Override
	public boolean isFocus(Long receiveId) {
		LambdaQueryWrapper<Focus> selectWrapper = new QueryWrapper<Focus>().lambda()
				.select(Focus::getReceiveId)
				.eq(Focus::getUserId, SecurityUtils.getUserId())//当前用户Id
				.eq(Focus::getReceiveId, receiveId);//被关注人Id
		return StringUtils.isNotNull(this.getOne(selectWrapper));
	}

	/**
	 * 获取我的粉丝
	 *
	 * @return
	 */
	@Override
	public List<Focus> myFans(Focus focus) {
		focus.setReceiveId(SecurityUtils.getUserId());
		return this.selectFocusList(focus);
	}

	/**
	 * 获取我关注的
	 */
	@Override
	public List<Focus> myFocus(Focus focus) {
		focus.setUserId(SecurityUtils.getUserId());
		return focusMapper.selectMyFocusList(focus);
	}
}
